package net.digitalartwork.ui{
	//import flash.filters.DropShadowFilter;
	//import fl.transitions.*;
	//import fl.transitions.easing.*;
	import flash.display.Stage;
	import flash.display.MovieClip;
	import flash.events.*;
	import flash.display.*;
	import flash.text.*;
	import net.digitalartwork.text.*;
	import net.digitalartwork.ui.SunDraw;
	import net.digitalartwork.ui.SimpleProgressBar;
	import gs.TweenFilterLite;
	import fl.motion.easing.*
	
	import flash.system.LoaderContext;
	import flash.system.ApplicationDomain;
	import flash.net.*;
	import flash.geom.Point;
	import flash.utils.*;

	

	public class TooltipMap extends Sprite{
		var toolText:TextMaker = new TextMaker;
		var expandedText:TextMaker = new TextMaker;
		var theTip:Sprite = new Sprite();
		public var bg:Sprite;
		var expandedMC:Sprite;
		var expandedBG:SunDraw;
		var currentText:String;
		var currentFont:String;
		var currentColor1:Number;
		var currentColor2:Number;
		var simpleBar:SimpleProgressBar;
		var barAmount:Number;
		var expanded_tipbox:MovieClip;
		var tipWidth:Number;
		var extendedTextParam:Object;
		var barWidth:Number;
		var expanedAssetLoaded:Boolean = false;
		var barTopNum:Number = 500000;
		public var varObjectHolder:Object;
		
		public var DEFLATE_TIP:String = "deflatetip";
		public var SEARCH_NEIGHBORS:String = "searchneighbors";
		
		
		var holdExWidth:Number
		var holdExHeight:Number
		var holdColor:Number
		var holdingPattern:Boolean = false;
		
		public function populateVarObjectHolder(theObj:Object):void {
			
		}
		function stringNumberComma(n:String):String {
			var s1:String = n;
			var a1:Array = s1.split(".");
			var s2:String = "";
			var l = a1[0].length;
			while(l--) {
				if(l < a1[0].length - 1 && (a1[0].length - l - 1) % 3 == 0) s2 = "," + s2;
				s2 = a1[0].substr(l, 1) + s2;
			}
			if(a1.length > 1) s2 += "." + a1[1];
				return(s2);
		}
		
		public function TooltipMap(hex:Number,hex2:Number, myFont:String ) {
			
			
			
			
			currentColor1 = hex;
			currentColor2 = hex2;
			currentFont = myFont;
			bg = new Sprite();
			theTip.name = "tooltip";
			addChild(theTip);

			//var toolText:TextMaker = new TextMaker;
			toolText.returnText(100, 20, 200, 20);
			toolText.selectable=false;
			toolText.autoSize = "left";
			toolText.antiAliasType = AntiAliasType.ADVANCED;
			theTip.addChild(toolText);
			
			toolText.text = "This is SHIZIT";
			toolText.x = 5;
			toolText.y = 10;
			
			var format:TextFormat = new TextFormat();
			format.font = myFont;
			format.color = hex2;
			format.size = 10;
			//format.underline = true;
			toolText.defaultTextFormat = format;
			
			bg.name = "background";
			theTip.addChildAt(bg, 0);

			bg.graphics.beginFill(0xFFFFFF);
			bg.graphics.lineStyle(1, 0x000000, toolText.textWidth);
			bg.graphics.moveTo(0, 0);
			bg.graphics.lineTo(toolText.textWidth+15, 0);
			bg.graphics.lineTo(toolText.textWidth+15, toolText.textHeight+10);
			bg.graphics.lineTo(20, toolText.textHeight+10);
			bg.graphics.lineTo(15, toolText.textHeight+20);
			bg.graphics.lineTo(10, toolText.textHeight+10);
			bg.graphics.lineTo(0, toolText.textHeight+10);
			bg.graphics.lineTo(0, 0);
			bg.graphics.endFill();
			theTip.visible = false;
			tipWidth = toolText.textWidth+15
			expandedMC = new Sprite();
			theTip.addChildAt(expandedMC, 0);
			expandedMC.visible = false;
			theTip.addChildAt(bg, 0);
			//--------------------------------------------------------------------------
			// EXTERNAL ASSETS
			//--------------------------------------------------------------------------
			var assetLoader = new Loader();
			assetLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, loaded);
			var assetContext:LoaderContext = new LoaderContext(false, ApplicationDomain.currentDomain);
			var request:URLRequest = new URLRequest("tooltipmap_assets.swf");
			assetLoader.load(request, assetContext);
			
			function loaded(e:Event):void {
				//--------------------------------------------------------------------------
				// SEARCH BOX
				//--------------------------------------------------------------------------
				
				var expandedTipBox:Class = getDefinitionByName("expandedTipBox") as Class;
				var expandedtipbox = new expandedTipBox();
				expanded_tipbox = expandedtipbox;
				expandedMC.addChild(expanded_tipbox);
				expanedAssetLoaded = true;
				
				if (holdingPattern) {
					expandTip(holdExWidth, holdExHeight, holdColor)
				}
				//searchmc.x = 655 - searchmc.width - 15;
				//searchmc.x = 20
				//searchmc.y = 450 - 192 - 20;
				//search_mc.searchButton.addEventListener(MouseEvent.CLICK, searchButtonClickHandler);
			}
		}
		public function expandTip(exWidth:Number, exHeight:Number, theColor:Number):void {
			if (expanedAssetLoaded) {
				trace("expandTip")
				theTip.removeChild(expandedMC);
				expandedMC = new Sprite();
				expandedMC.addChild(expanded_tipbox);
				theTip.addChildAt(expandedMC, 0);
				expandedMC.visible = true;

				expanded_tipbox.bathroomNumber.text = extendedTextParam.bathroomNumber;
				expanded_tipbox.bathroomHalfNumber.text = extendedTextParam.bathroomHalfNumber;
				expanded_tipbox.sqfeetNumber.text = stringNumberComma(extendedTextParam.sqfeetNumber);
				expanded_tipbox.annualWaterText.text = stringNumberComma(extendedTextParam.annualWaterText);
				expanded_tipbox.waterMiscText.text = extendedTextParam.waterMiscText;
				expanded_tipbox.poolNumber.text = extendedTextParam.poolNumber;
				//expanded_tipbox.sprinklerText.text = extendedTextParam.sprinklerText;
				expanded_tipbox.sprinklerNumber.text = extendedTextParam.sprinklerNumber;
				

				var expandHeight:Number = expandedMC.height;
				var expandWidth:Number = expandedMC.width;
				expandedMC.y = -20;
				expandedMC.scaleX = 0.1;
				expandedMC.scaleY = 0.1;
				expanded_tipbox.waterBar.amount_bar.width = 1;
				TweenFilterLite.to(expandedMC, .5, {x:-10, y:-expandHeight - 10, scaleX:1, scaleY:1, alpha:1, ease:Back.easeOut});
				var annualWaterNum:Number = extendedTextParam.annualWaterText
				if (annualWaterNum > 500000) {
					barTopNum = 1000000;
				} else {
					barTopNum = 500000;
				}
				if (annualWaterNum > 1000000) {
					barTopNum = 7000000;
				}
				barAmount = (annualWaterNum/barTopNum)*100;
				expanded_tipbox.waterBar.usAvg.x = Math.round((220000/barTopNum)*100);
				expanded_tipbox.waterBar.graphTopNum.text = stringNumberComma(barTopNum.toString());
				//barAmount = (extendedTextParam.annualWaterText/500000)*100;
				if (barTopNum > 500000) {
					//barAmount = 100;
					TweenFilterLite.to(expanded_tipbox.waterBar.amount_bar, .5, {width:barAmount, delay:.5, alpha:1, colorMatrixFilter:{colorize:0xff0000, amount:1}, ease:Linear.easeOut});
				} else {
					TweenFilterLite.to(expanded_tipbox.waterBar.amount_bar, .5, {width:barAmount, delay:.5, alpha:1, ease:Linear.easeOut});
				}
				

				expanded_tipbox.closeButton.addEventListener(MouseEvent.MOUSE_DOWN, closeHandeler);
				expanded_tipbox.closeButton.addEventListener(MouseEvent.MOUSE_OVER, closeOverHandeler);
				expanded_tipbox.closeButton.addEventListener(MouseEvent.MOUSE_OUT, closeOutHandeler);
				
				expanded_tipbox.viewNeighborsBtn.addEventListener(MouseEvent.MOUSE_DOWN, neighborBtnDownHandeler);
				expanded_tipbox.viewNeighborsBtn.addEventListener(MouseEvent.MOUSE_OVER, neighborBtnOverHandeler);
				expanded_tipbox.viewNeighborsBtn.addEventListener(MouseEvent.MOUSE_OUT, neighborBtnOutHandeler);
				
				function closeHandeler(event:Event):void {
					
					deflateTip();
					dispatchEvent(new Event(DEFLATE_TIP));
				}
				function closeOverHandeler(event:Event):void {
					expanded_tipbox.closeButton.bg.alpha = .5;
				}
				function closeOutHandeler(event:Event):void {
					expanded_tipbox.closeButton.bg.alpha = 1;
				}
				function neighborBtnDownHandeler(event:Event):void {
					//deflateTip();
					dispatchEvent(new Event(SEARCH_NEIGHBORS));
				}
				function neighborBtnOverHandeler(event:Event):void {
					expanded_tipbox.viewNeighborsBtn.bg.alpha = .75;
				}
				function neighborBtnOutHandeler(event:Event):void {
					expanded_tipbox.viewNeighborsBtn.bg.alpha = 1;
				}
			} else {
				holdExWidth = exWidth;
				holdExHeight = exHeight;
				holdColor = theColor;
				holdingPattern = true;
			}
			
			
		}
		public function populateTip(et:Object):void {
			extendedTextParam = et;
			//barWidth = barPercent*100;
			//waterBar.amount_bar.width = barPercent*100;
			/*
			expanded_tipbox.bathroomNumber.text = "0";
			expanded_tipbox.bathroomText.text = "0";
			expanded_tipbox.sqfeetNumber.text = "0";
			expanded_tipbox.sqfeetText.text = "0";
			expanded_tipbox.poolNumber.text = "0";
			expanded_tipbox.poolText.text = "0";
			expanded_tipbox.waterMiscText.text = "0";
			*/
			//expandedText.text = theText;
			//simpleBar.updateProgress(barPercent);
			//barAmount = barPercent;
			
		}
		public function deflateTip():void {
			TweenFilterLite.to(expandedMC, .5, {x:0, y:-bg.height, scaleX:.1, scaleY:.1, alpha:0, ease:Back.easeOut});
		}
		public function showTip(theText:String):void {
			//delete bg;
			addChild(theTip);
			theTip.removeChild(bg);
			toolText.selectable=false;
			toolText.mouseEnabled = false;
			toolText.text = theText;
			currentText = theText
			//bg = null;
			bg = new Sprite();
			theTip.addChildAt(bg, 0);
			bg.name = "background";
			
			theTip.addChildAt(bg, 0);
			
			bg.graphics.beginFill(0xFFFFFF);
			bg.graphics.lineStyle(1, 0x000000, toolText.textWidth);
			bg.graphics.moveTo(0, 0);
			bg.graphics.lineTo(toolText.textWidth+15, 0);
			bg.graphics.lineTo(toolText.textWidth+15, toolText.textHeight+10);
			bg.graphics.lineTo(20, toolText.textHeight+10);
			bg.graphics.lineTo(15, toolText.textHeight+20);
			bg.graphics.lineTo(10, toolText.textHeight+10);
			bg.graphics.lineTo(0, toolText.textHeight+10);
			bg.graphics.lineTo(0, 0);
			bg.graphics.endFill();
			
			bg.x = -10;
			bg.y = 0 - bg.height;
			toolText.x = bg.x+5;
			toolText.y = bg.y+2;
			//theTip.x = 200;
			//theTip.y = 200;
			
			
			theTip.visible = true;
			theTip.alpha = 0;
			theTip.scaleX = 0;
			theTip.scaleY = 0;
			TweenFilterLite.to(theTip, .5, {scaleX:1, scaleY:1, alpha:1, ease:Back.easeOut, dropShadowFilter:{color:0x000000, alpha:.75, blurX:5, blurY:5, angle:45, distance:3}});
			
		}
		public function highlightTip(theColor:Number, theTextColor:Number, theScale:Number):void {
			TweenFilterLite.to(bg, .5, {colorMatrixFilter:{colorize:theColor, amount:1}});
			TweenFilterLite.to(theTip, .5, {scaleX:theScale, scaleY:theScale, alpha:1, ease:Back.easeOut});
			TweenFilterLite.to(toolText, .5, {colorMatrixFilter:{colorize:theTextColor, amount:1}});
		}
		public function changeTip(theText:String):void {
			//delete bg;
			addChild(theTip);
			theTip.removeChild(bg);
			toolText.text = theText;
			//bg = null;
			bg = new Sprite();
			theTip.addChildAt(bg, 0);
			bg.name = "background";
			
			theTip.addChildAt(bg, 0);
			
			bg.graphics.beginFill(0xFFFFFF);
			bg.graphics.lineStyle(1, 0x000000, toolText.textWidth);
			bg.graphics.moveTo(0, 0);
			bg.graphics.lineTo(toolText.textWidth+15, 0);
			bg.graphics.lineTo(toolText.textWidth+15, toolText.textHeight+10);
			bg.graphics.lineTo(20, toolText.textHeight+10);
			bg.graphics.lineTo(15, toolText.textHeight+20);
			bg.graphics.lineTo(10, toolText.textHeight+10);
			bg.graphics.lineTo(0, toolText.textHeight+10);
			bg.graphics.lineTo(0, 0);
			bg.graphics.endFill();
			
			bg.x = -10;
			bg.y = 0 - bg.height;
			toolText.x = bg.x+5;
			toolText.y = bg.y+2;
			//theTip.x = 200;
			//theTip.y = 200;
			
			
			theTip.visible = true;

			//TweenFilterLite.to(theTip, .5, {scaleX:1, scaleY:1, alpha:1, ease:Back.easeOut, dropShadowFilter:{color:0x000000, alpha:.75, blurX:5, blurY:5, angle:45, distance:3}});
			
		}
		public function removeTip():void {
			removeChild(theTip);
		}
		public function getText():String {
			return this.currentText;
			//return "dude"
		}
	}
}




